package LeetCode;

import java.util.Random;

public class LC_398_RandomPickIndex {

    public static void main(String[] args) {

    }

    public class Solution {
        private int[] nums;
        private Random random;

        public Solution(int[] nums) {
            this.nums = nums;
            this.random = new Random();
        }

        public int pick(int target) {
            int result = -1;
            int upbound = 1;
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] == target) {
                    if (random.nextInt(upbound) == 0) result = i;
                    upbound++;
                }
            }
            return result;
        }

    }
}